package com.futures.investm.common;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.futures.investm.R;
import com.futures.investm.utils.CacheData;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.UpdatePasswordCallback;

import butterknife.BindView;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;

public class ChangePasswordActivity extends BaseTitleActivity {

    private final int ERROR_CODE_INCORRECT_OLD_PASSWORD = 210;//旧密码有误

    @BindView(R.id.et_old_pw)
    EditText etOldPw;
    @BindView(R.id.et_new_pw)
    EditText etNewPw;
    @BindView(R.id.et_comfirm_pw)
    EditText etComfirmPw;

    private AVUser mUser;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("密码设置")
                .setOnLeftTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                })
                .setStatusBarLightMode(false)
                .setRightText("完成")
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sure();
                    }
                });
    }


    @Override
    public int getContentLayout() {
        return R.layout.activity_change_password;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mUser = CacheData.getLoginUser();
    }

    public void sure() {
        String oldPw = etOldPw.getText().toString();
        final String newPw = etNewPw.getText().toString();
        String comfirmPw = etComfirmPw.getText().toString();

        //旧密码校验
        if (DataUtils.isEmptyString(oldPw)) {
            ToastUtil.show("请输入旧密码");
            return;
        }

        //新密码校验
        if (DataUtils.isEmptyString(newPw)) {
            ToastUtil.show("请输入新密码");
            return;
        }

        //新密码校验
        if (DataUtils.isEmptyString(comfirmPw)) {
            ToastUtil.show("请输入确认密码");
            return;
        }

        if (newPw.length() < 6) {
            ToastUtil.show("新密码不能少于6位");
            return;
        }

        if (!newPw.equals(comfirmPw)) {
            ToastUtil.show("新密码与确认密码不一致");
            return;
        }

        mUser.updatePasswordInBackground(oldPw, newPw, new UpdatePasswordCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    String password = mUser.getString("password");
                    CacheData.setLoginPassword(mContext, newPw);
                    ToastUtil.show("密码更改成功");
                    finish();
                } else {
                    if (ERROR_CODE_INCORRECT_OLD_PASSWORD == e.getCode()) {
                        ToastUtil.show("旧密码不正确");
                    } else {
                        ToastUtil.show("密码更改失败");
                    }
                }
            }
        });
        finish();
    }
}
